package com.zaoke.web.dao.food.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

import com.zaoke.web.bo.food.FoodImage;
import com.zaoke.web.dao.JdbcSupport;
import com.zaoke.web.dao.food.FoodImageDao;

public class FoodImageDaoImpl extends JdbcSupport implements FoodImageDao{

	private static final String TABLE = "`zaoke`.`food_image`";
	private static final String QUERY_VIEW = "`id`, `food_id`, `url`, `add_userid`, `add_time`, `lastmod_userid`, `lastmod_time`, `type`";

	public List<FoodImage> getAllImages() {
		String sql = "select " + QUERY_VIEW + " from " + TABLE;
		return _query(sql,false);
	}

	public List<FoodImage> getImagesByFoodIds(Set<Long> remainings,boolean containContent) {
		String sql = "";
		if(!containContent)
		{
			sql = "select " + QUERY_VIEW + " from " + TABLE + " where `food_id` in (";	
		}
		else
		{
			sql = "select " + QUERY_VIEW + ",`content` from " + TABLE + " where `food_id` in (";
		}
		
		sql += StringUtils.join(remainings, ",");
		sql += ")";
		return _query(sql,containContent);
	}

	private List<FoodImage> _query(String sql,final boolean containContent) {
		List<FoodImage> images = getTemplateMgr().getReadTp().query(sql, new RowMapper<FoodImage>() {

			public FoodImage mapRow(ResultSet rs, int rowNum) throws SQLException {
				FoodImage image = new FoodImage();
				image.setId(rs.getLong("id"));
				image.setFoodid(rs.getLong("food_id"));
				image.setUrl(rs.getString("url"));
				image.setAddUserid(rs.getLong("add_userid"));
				image.setAddTime(rs.getDate("add_time"));
				image.setLastmodUserid(rs.getLong("lastmod_userid"));
				image.setLastmodTime(rs.getDate("lastmod_time"));
				image.setType(rs.getInt("type"));
				if(containContent)
					image.setContent(rs.getString("content"));
				return image;
			}
		});
		return images;
	}

	@Override
	public boolean newFoodImage(FoodImage image) {
		
		if(image==null)
			return false;
		
		String sql = "insert into "+TABLE;
		sql += "(food_id,url,content,add_userid,add_time,lastmod_userid,lastmod_time, type)";
		sql += " values(?,?,?,?,now(),?,now(),?)";
		
		Long foodid = image.getFoodid();
		String content = image.getContent();
		String url = image.getUrl();
		Long userid = image.getUserid();
		
		Object[] params = new Object[]{foodid,url,content,userid,userid, image.getType()};
		
		return getTemplateMgr().getWriteTp().update(sql, params)>0;
	}

	@Override
	public List<FoodImage> getImageByFoodId(Long foodid, boolean containConent) {
		String sql = "";
		if(!containConent)
		{
			sql = "select " + QUERY_VIEW + " from " + TABLE + " where `food_id`=";	
		}
		else
		{
			sql = "select " + QUERY_VIEW + ",`content` from " + TABLE + " where `food_id`=";
		}
		sql += foodid;
		return _query(sql,containConent);
	}

	@Override
	public boolean updateFoodImage(FoodImage image) {
		if(image==null)
			return false;
		
//		Long foodid = image.getFoodid();
		String content = image.getContent();
		String url = image.getUrl();
		Long userid = image.getUserid();
		
		String sql = "replace into "+TABLE+" set lastmod_time=now(),add_time=now() ";
		
		List<Object> params = new LinkedList<Object>();
		if(url!=null && !"".equals(url))
		{
			sql += " , url=? ";
			params.add(url);
		}
		if(content!=null && !"".equals(content))
		{
			sql += " , content=? ";
			params.add(content);
		}
		if(userid!=null)
		{
			sql += " , lastmod_userid=? ";
			params.add(userid);
			sql += " , add_userid=? ";
			params.add(userid);
		}
		sql += ", food_id = ?";
		params.add(image.getFoodid());
		sql += ", type = ?";
		params.add(image.getType());
		//sql += " where food_id=? and type = ?";
		return getTemplateMgr().getWriteTp().update(sql, params.toArray())>0;
		
	}

	@Override
	public boolean deleteImage(Long foodid) {

		String sql = "delete from "+TABLE + " where food_id=?";
		
		return getTemplateMgr().getWriteTp().update(sql, new Object[]{foodid})>0;
	}
}
